package com.lwgk.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lwgk.R;
import com.lwgk.compinterface.BackCall;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/11
 * --------------------------------------------------------------------------
 * 描述：这是弹框对话框，全文同用，有带标题的弹框，不带标题的弹框，单按钮，多按钮弹框等
 * 注意事项：弹框的Content的使用，只能是当前界面的content，不能使用 AppComplication.mContext
 * 加载弹框的布局，平板和手机共用同一个，限制宽度为250dp
 * 在setCancelable(false)时按返回键不可dismiss
 */
public class DialogUtils {

    /**
     * 这是，带标题的单按钮弹框
     *
     * @param context        上下文环境
     * @param titel          标题,如果标题为null或者""的时候，自动隐藏标题
     * @param content        中间的描述
     * @param confirm        确认按钮
     * @param outsidedismiss 点击外部是否可以取消，true为可以取消，false为不可以取消
     * @param back           点击确认按钮返回操作
     * @return
     */
    public static Dialog showTitleSingleSelect(final Context context, String titel, String content, String confirm, boolean outsidedismiss, final BackCall back) {
        final Dialog dlg = new Dialog(context, R.style.showDialogTheme);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_show_single_select, null);
        TextView title = (TextView) layout.findViewById(R.id.title);
        View view = (View) layout.findViewById(R.id.view);
        if (titel.isEmpty()||titel==null) {
            title.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
        } else {
            title.setVisibility(View.VISIBLE);
            view.setVisibility(View.VISIBLE);
            title.setText(titel + "");
        }
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText(content + "");
        TextView mConfirm = layout.findViewById(R.id.confirm);
        mConfirm.setText(confirm + "");
        layout.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back.deal(R.id.confirm, 0);
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        //点击外部是否可以取消
        if (outsidedismiss) {
            dlg.setCanceledOnTouchOutside(true);
        } else {
            dlg.setCanceledOnTouchOutside(false);
        }
        dlg.setContentView(layout);
        //在setCancelable(false)时按返回键可dismiss
        dlg.setCancelable(true);
        return dlg;
    }

    /**
     * 这是，带标题的双按钮弹框，右边带颜色
     *
     * @param context        上下文环境
     * @param titel          标题,如果标题为null或者""的时候，自动隐藏标题
     * @param content        中间的描述
     * @param confirm        确认按钮
     * @param outsidedismiss 点击外部是否可以取消，true为可以取消，false为不可以取消
     * @param back           点击确认按钮返回操作
     * @return
     */
    public static Dialog showTitleDoubleRightSelect(final Context context, String titel, String content, String confirm,String cancle ,boolean outsidedismiss, final BackCall back) {
        final Dialog dlg = new Dialog(context, R.style.showDialogTheme);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_show_double_right_select, null);
        TextView title = (TextView) layout.findViewById(R.id.title);
        View view = (View) layout.findViewById(R.id.view);
        if (titel.isEmpty()||titel==null) {
            title.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
        } else {
            title.setVisibility(View.VISIBLE);
            view.setVisibility(View.VISIBLE);
            title.setText(titel + "");
        }
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText(content+"");
        TextView mConfirm= layout.findViewById(R.id.confirm);
        mConfirm.setText(confirm+"");
        TextView mCancle=layout.findViewById(R.id.concel);
        mCancle.setText(cancle+"");
        layout.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back.deal(R.id.confirm,1);
                dlg.dismiss();
            }
        });
        layout.findViewById(R.id.concel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back.deal(R.id.concel,2);
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dlg.onWindowAttributesChanged(lp);
        //点击外部是否可以取消
        if (outsidedismiss) {
            dlg.setCanceledOnTouchOutside(true);
        } else {
            dlg.setCanceledOnTouchOutside(false);
        }
        dlg.setContentView(layout);
        //在setCancelable(false)时按返回键可dismiss
        dlg.setCancelable(true);
        return dlg;
    }
    /**
     * 这是，带标题的双按钮弹框，左边带颜色，目的是方便用户点击确定
     * @param context        上下文环境
     * @param titel          标题,如果标题为null或者""的时候，自动隐藏标题
     * @param content        中间的描述
     * @param confirm        确认按钮
     * @param outsidedismiss 点击外部是否可以取消，true为可以取消，false为不可以取消
     * @param back           点击确认按钮返回操作
     * @return
     */
    public static Dialog showTitleDoubleLiftSelect(final Context context, String titel, String content, String confirm,String cancle ,boolean outsidedismiss, final BackCall back) {
        final Dialog dlg = new Dialog(context, R.style.showDialogTheme);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_show_double_lift_select, null);
        TextView title = (TextView) layout.findViewById(R.id.title);
        View view = (View) layout.findViewById(R.id.view);
        if (titel.isEmpty()||titel==null) {
            title.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
        } else {
            title.setVisibility(View.VISIBLE);
            view.setVisibility(View.VISIBLE);
            title.setText(titel + "");
        }
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText(content+"");
        TextView mConfirm= layout.findViewById(R.id.confirm);
        mConfirm.setText(confirm+"");
        TextView mCancle=layout.findViewById(R.id.concel);
        mCancle.setText(cancle+"");
        layout.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back.deal(R.id.confirm,1);
                dlg.dismiss();
            }
        });
        layout.findViewById(R.id.concel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back.deal(R.id.concel,2);
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dlg.onWindowAttributesChanged(lp);
        //点击外部是否可以取消
        if (outsidedismiss) {
            dlg.setCanceledOnTouchOutside(true);
        } else {
            dlg.setCanceledOnTouchOutside(false);
        }
        dlg.setContentView(layout);
        //在setCancelable(false)时按返回键可dismiss
        dlg.setCancelable(true);
        return dlg;
    }

}
